const db = require("../config/db");

exports.getAll = async () => {
  const [rows] = await db.query("SELECT * FROM products");
  return rows;
};

exports.add = async (product) => {
  const { pname, price, supplier, habitat, type } = product;
  const [result] = await db.query(
    "INSERT INTO products (pname, price, supplier, habitat, type) VALUES (?, ?, ?, ?, ?)",
    [pname, price, supplier, habitat, type]
  );
  return { id: result.insertId, ...product };
};

exports.update = async (id, product) => {
  const { pname, price, supplier, habitat, type } = product;
  await db.query(
    "UPDATE products SET pname=?, price=?, supplier=?, habitat=?, type=? WHERE id=?",
    [pname, price, supplier, habitat, type, id]
  );
  return { id, ...product };
};

exports.remove = async (id) => {
  await db.query("DELETE FROM products WHERE id=?", [id]);
};
